package cn.lystudio.rohm_time_op.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.lystudio.rohm_time_op.entity.dto.MachineDTO;
import cn.lystudio.rohm_time_op.entity.dto.OpItemDTO;
import cn.lystudio.rohm_time_op.entity.po.DepartmentsPO;
import cn.lystudio.rohm_time_op.entity.po.MachinePO;
import cn.lystudio.rohm_time_op.entity.po.OpItemPO;
import cn.lystudio.rohm_time_op.entity.po.QrTypePO;
import cn.lystudio.rohm_time_op.mapper.OpItemPOMapper;
import cn.lystudio.rohm_time_op.mapper.QrTypePOMapper;
import cn.lystudio.rohm_time_op.service.MachineService;
import cn.lystudio.rohm_time_op.service.OpItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class OpItemServiceImpl implements OpItemService {
    @Resource
    private OpItemPOMapper opItemPOMapper;
    @Resource
    private QrTypePOMapper qrTypePOMapper;

    private MachineService machineService;

    @Lazy
    @Autowired
    public void setMachineService(MachineService machineService) {
        this.machineService = machineService;
    }

    /**
     * 鉴权,获取所有作业项目,qrType,制造工程,设备
     *
     * @return
     */
    @Override
    public List<OpItemDTO> getAll(String keyword) {
        // 1.获取设备权限(已鉴权)
        List<MachineDTO> machineDTOList = machineService.getAll(null);
        // 1.1 过滤machine.enabled=true数据
        CollUtil.filter(machineDTOList, machineDTO -> {
            return machineDTO.getMachine().getEnabled();
        });
        // 1.2.组装 secDeptIDList和 machineNoList
        Set<Integer> secDeptIDList = new ConcurrentHashSet<>();
        Set<DepartmentsPO> secDeptList = new ConcurrentHashSet<>();
        Set<String> machineNoList = new ConcurrentHashSet<>();
        CollUtil.forEach(machineDTOList.iterator(), (machineDTO, index) -> {
            secDeptIDList.add(machineDTO.getSecDept().getDepartmentID());
            secDeptList.add(machineDTO.getSecDept());
            machineNoList.add(machineDTO.getMachine().getMachineNo());
        });
        if (CollUtil.isEmpty(secDeptIDList) || CollUtil.isEmpty(machineNoList) || CollUtil.isEmpty(secDeptList))
            return null;
        // 2.获取作业者作业项目 opItemList
        LambdaQueryWrapper<OpItemPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(OpItemPO::getSecDeptID, secDeptIDList)
                .or()
                .in(OpItemPO::getBelong2MacNo, machineNoList);
        List<OpItemPO> opItemPOList = opItemPOMapper.selectList(wrapper);
        // 3.获取所有qrType
        LambdaQueryWrapper<QrTypePO> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(QrTypePO::getEnabled, true);
        List<QrTypePO> qrTypePOList = qrTypePOMapper.selectList(wrapper1);
        // 4.遍历组装view层数据
        List<OpItemDTO> opItemDTOList = CollUtil.list(false);
        CollUtil.forEach(opItemPOList.iterator(), (opItem, index) -> {
            OpItemDTO opItemDTO = new OpItemDTO();
            opItemDTO.setOpItem(opItem);
            if (ObjectUtil.isNotEmpty(opItem.getQrTypeID())) {
                opItemDTO.setQrType(
                        CollUtil.findOne(qrTypePOList, qrTypePO -> {
                            return opItem.getQrTypeID().equals(qrTypePO.getQrTypeID());
                        })
                );
            }
            opItemDTO.setSecDept(
                    CollUtil.findOne(secDeptList, secDept -> {
                        return secDept.getDepartmentID().equals(opItem.getSecDeptID());
                    })
            );
            if (StrUtil.isNotEmpty(opItem.getBelong2MacNo())) {
                opItemDTO.setMachine(
                        CollUtil.findOne(machineDTOList, machineDTO -> {
                            return opItem.getBelong2MacNo().equals(machineDTO.getMachine().getMachineNo());
                        })
                );
            }
            opItemDTOList.add(opItemDTO);
        });
        // 5.过滤数据
        CollUtil.removeNull(opItemDTOList);
        CollUtil.filter(opItemDTOList, opItemDTO -> {
            return (ObjectUtil.isNotEmpty(opItemDTO.getSecDept()));
        });

        // 6.搜索过滤
        CollUtil.filter(opItemDTOList, opItemDTO -> {
            return opItemDTO.getOpItem().getOpItemName().contains(keyword);
        });

        return opItemDTOList;
    }

    @Override
    public int uptOpItemEnabled(Integer opItemID, Boolean enabled) {
        OpItemPO opItemPO = new OpItemPO();
        opItemPO.setOpItemID(opItemID);
        opItemPO.setEnabled(enabled);
        return opItemPOMapper.updateById(opItemPO);
    }

    @Override
    public int delOpItem(Integer opItemID) {
        return opItemPOMapper.deleteById(opItemID);
    }

    @Override
    public int addOpItem(OpItemPO opItemPO) {
        opItemPO.setOpItemContent(Convert.toDBC(opItemPO.getOpItemContent()));
        return opItemPOMapper.insert(opItemPO);
    }

    @Override
    public int uptOpItem(OpItemPO opItemPO) {
        opItemPO.setOpItemContent(Convert.toDBC(opItemPO.getOpItemContent()));
        return opItemPOMapper.updateById(opItemPO);
    }

    @Override
    public List<OpItemPO> getOpItemListByMachineNo(String machineNo) {
        //1.通过设备获取secDeptID
        MachinePO machinePO = machineService.get1ByMachineNo_Usable(machineNo);
        if (ObjectUtil.isEmpty(machinePO)) return null;
        //2. 获取数据
        LambdaQueryWrapper<OpItemPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OpItemPO::getSecDeptID, machinePO.getSecDeptID())
                .or()
                .eq(OpItemPO::getBelong2MacNo, machinePO.getMachineNo());

        return opItemPOMapper.selectList(wrapper);
    }

    @Override
    public OpItemPO getOpitemByOpItemNameAndMachineNo(String opItemName, String machineNo) {
        //1.通过设备获取secDeptID
        MachinePO machinePO = machineService.get1ByMachineNo_Usable(machineNo);
        if (ObjectUtil.isEmpty(machinePO)) return null;
        //2.查询数据
        LambdaQueryWrapper<OpItemPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OpItemPO::getOpItemName, opItemName)
                .and(i -> {
                    i.eq(OpItemPO::getSecDeptID, machinePO.getSecDeptID())
                            .or()
                            .eq(OpItemPO::getBelong2MacNo, machinePO.getMachineNo());
                })
                .last("LIMIT 1");

        return opItemPOMapper.selectOne(wrapper);
    }
}
